home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 09 Laramée / WorldGrid.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-22  |  2.8 KB  |  129 lines

  1. /****************************************************************
  2.  * WorldGrid Implementation
  3.  ***************************************************************/
  4.  
  5. #include "WorldGrid.h"
  6. #include "Globals.h"
  7. #include <iostream.h>
  8. #include <math.h>
  9.  
  10.  
  11. // Construction
  12. WorldGrid::WorldGrid()
  13. {
  14.     for( int i = 0; i < MAX_ENTITIES; i++ )
  15.     {
  16.         Registry[ i ].type = ENTITY_NULL;
  17.     }
  18. }
  19.  
  20. WorldGrid::WorldGrid( int x, int y )
  21. {
  22.     TrollX = x;
  23.     TrollY = y;
  24.     for( int i = 0; i < MAX_ENTITIES; i++ )
  25.     {
  26.         Registry[ i ].type = ENTITY_NULL;
  27.     }
  28. }
  29.  
  30. void WorldGrid::Copy( WorldGrid * src )
  31. {
  32.     for( int i = 0; i < MAX_ENTITIES; i++ )
  33.     {
  34.         Registry[ i ].type = src->Registry[ i ].type;
  35.         Registry[ i ].x = src->Registry[ i ].x;
  36.         Registry[ i ].y = src->Registry[ i ].y;
  37.     }
  38. }
  39.  
  40.  
  41. // Check whether the troll is close enough to trigger activity
  42. bool WorldGrid::IsTrollWithinRange( int id )
  43. {
  44.     int manhattan = DistanceFromTroll( id );
  45.     switch( Registry[ id ].type )
  46.     {
  47.         case ENTITY_SHEEP:
  48.             return ( manhattan <= 2 ) ? true : false; break;
  49.         case ENTITY_TOWER:
  50.             return ( manhattan <= 4 ) ? true : false; break;
  51.         case ENTITY_KNIGHT:
  52.             return ( manhattan <= 3 ) ? true : false; break;
  53.         case ENTITY_HAVEN:
  54.         case ENTITY_TRAP:
  55.             return ( manhattan == 0 ) ? true : false; break;
  56.         default:
  57.             return false; break;
  58.     }
  59. }
  60.  
  61.  
  62. // Movement on the game world grid
  63. void WorldGrid::MoveTroll( int newx, int newy )
  64. {
  65.     TrollX = newx;
  66.     TrollY = newy;
  67. }
  68.  
  69. void WorldGrid::MoveEntity( int registryIndex, int newX, int newY )
  70. {
  71.     Registry[ registryIndex ].x = newX;
  72.     Registry[ registryIndex ].y = newY;
  73. }
  74.  
  75.  
  76. // Add an entity to the registry
  77. void WorldGrid::Register( int ID, int type, int x, int y )
  78. {
  79.     Registry[ ID ].type = type;
  80.     Registry[ ID ].x = x;
  81.     Registry[ ID ].y = y;
  82. }
  83.  
  84.  
  85. // Print the contents of the registry
  86. void WorldGrid::Dump()
  87. {
  88.     cout << "DUMP OF WORLDGRID:" << endl;
  89.     for( int i = 0; i < MAX_ENTITIES; i++ )
  90.     {
  91.         if( Registry[ i ].type != ENTITY_NULL )
  92.         {
  93.             cout << "ENTITY #" << i << " of type " << Registry[ i ].type << " at "
  94.                    << Registry[ i ].x << "," << Registry[ i ].y << endl;
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. // Look for the entity of class "type" closest to the troll
  101. int WorldGrid::FindClosestFromTroll( int type )
  102. {
  103.     int choice = -1;
  104.     int bestSoFar = 1000;
  105.  
  106.     for( int i = 0; i < MAX_ENTITIES; i++ )
  107.     {
  108.         if ( Registry[ i ].type == type && DistanceFromTroll( i ) < bestSoFar )
  109.         {
  110.             choice = i;
  111.             bestSoFar = DistanceFromTroll( i );
  112.         }
  113.     }
  114.  
  115.     return choice;
  116. }
  117.  
  118.  
  119. // Count objects of a certain kind close to the troll
  120. int WorldGrid::HowManyCloseToTroll( int type, int maxdist )
  121. {
  122.     int howMany = 0;
  123.     for( int i = 0; i < MAX_ENTITIES; i++ )
  124.     {
  125.         if ( Registry[ i ].type == type && DistanceFromTroll( i ) <= maxdist )
  126.             howMany++;
  127.     }
  128.     return howMany;
  129. }